﻿using TradePublish.Common;
using log4net;
using TradePublish.Service;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Threading;
using TradePublish.Common;
using TradePublish.Service;
using System.Data.OracleClient;

public class RegnoGifDao
{
    private ILog log = null;

    public RegnoGifDao()
    {
        this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    }

    public int getMaxISSUE()
    {
        string commandText = " select * from (select  ISSUE  as hasgrasp from REGNO_GIF  order by ISSUE  desc )  where ROWNUM<=1 order by ROWNUM asc";
        while (true)
        {
            if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30)
            {
                Thread.Sleep(180000);//3fenzhong  
                continue;
            }
            DataSet set = new DataSet();
            DatabaseHelper dbh = new DatabaseHelper();
            try
            {
                set = dbh.ExecuteDataSet( commandText );
                if (((set != null) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
                {
                    int num = Convert.ToInt32(set.Tables[0].Rows[0][0]);
                    if (num > 0)
                    {
                        this.log.Info("当前数据库最大公告期号:" + num);
                        return num;
                    }
                }
                this.log.Info("当前数据库中无最大公告期号");
                return -1;
            }
            catch (Exception exception)
            {
                this.log.Error("获得最大公告期号异常", exception);
                Console.WriteLine(exception.Message);
                Thread.Sleep(0x7530);
            }
            finally
            {
                dbh.Dispose();
            }
        }
    }

    public DataSet getNoFinishedOne()
    {
        while (true)
        {
            if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30)
            {
                Thread.Sleep(180000);//3fenzhong  
                continue;
            }
            break;
        }
        string commandText = "select *  from (select  *  from REGNO_GIF  where HAS_FINISHED = 0 order by ISSUE desc,category asc )  where ROWNUM<=1 order by ROWNUM asc ";

        DataSet set = new DataSet();
        DatabaseHelper dbh = new DatabaseHelper();
        try
        {
            set = dbh.ExecuteDataSet(commandText); 
        }
        catch (Exception exception)
        {
            this.log.Error("获得最大公告期号异常", exception); 
        }
        finally
        {
            dbh.Dispose();
        } 
        return set;
    }

    public void insertNewISSUE(int issue, DateTime issueDate, string[] pageStaEnd)
    {
        while (true)
        {
            if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30)
            {
                Thread.Sleep(180000);//3fenzhong  
                continue;
            }
            DatabaseHelper dbh = new DatabaseHelper();

             
            try
            {
                dbh.BeginTransaction();
                string cmdText = "INSERT INTO  REGNO_GIF (id,ISSUE,ISSUE_DATE,CATEGORY,START_PAGE_NO,END_PAGE_NO,HAS_FINISHED,synchroflag) Values(gen_sequence.nextval,:p1,:p2,:p3,:p4,:p5,0,0)";
                for (int i = 0; i < pageStaEnd.Length; i++)
                {
                    if (pageStaEnd[i] != null)
                    {
                       // Int64 id = dbh.getSequence();
                        foreach (OracleParameter parameter in new OracleParameter[] { new OracleParameter(":p1", issue), new OracleParameter(":p2", issueDate), new OracleParameter(":p3", i), new OracleParameter(":p4", Convert.ToInt32(pageStaEnd[i].Split(new char[] { '-' })[0])), new OracleParameter(":p5", Convert.ToInt32(pageStaEnd[i].Split(new char[] { '-' })[1])) })
                        {
                            if ((parameter.Direction == ParameterDirection.InputOutput) && (parameter.Value == null))
                            {
                                parameter.Value = DBNull.Value;
                            }
                            dbh.AddParameter( parameter);
                        }
                        dbh.ExecuteNonQuery(cmdText,TradePublish.Service.ConnectionState.KeepOpen);
                    }
                }
                dbh.CommitTransaction();
                this.log.Info("向数据库表REGNO_GIF表成功插入新的公告期" + issue);
                return;
            }
            catch (Exception exception)
            {
                this.log.Error("向数据库表REGNO_GIF表插入新的公告期" + issue + "失败！", exception);
                 dbh.RollbackTransaction();  
               
            }
            finally
            {
                dbh.Dispose();
            }
            Thread.Sleep(0x7530);
        }
    }

    public void updateFinish(int id)
    {
        while (true)
        {
            if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30)
            {
                Thread.Sleep(180000);//3fenzhong  
                continue;
            }
            break;
        }
        string commandText = "update REGNO_GIF  set HAS_FINISHED =1  where id=" + id;
 
        DatabaseHelper dbh = new DatabaseHelper();
        try
        {
            dbh.ExecuteNonQuery(commandText); 
        }
        catch (Exception exception)
        {
            this.log.Error("设置REGNO_GIF已完成异常", exception); 
        }
        finally
        {
            dbh.Dispose();
        }  
    }

    public void updatePageNo(int id, int pageNo)
    {
        while (true)
        {
            if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30)
            {
                Thread.Sleep(180000);//3fenzhong  
                continue;
            }
            break;
        }
        string commandText = string.Concat(new object[] { "update REGNO_GIF  set START_PAGE_NO = ", pageNo, " where id=", id });
        DatabaseHelper dbh = new DatabaseHelper();
        try
        {
            dbh.ExecuteNonQuery(commandText);
        }
        catch (Exception exception)
        {
            this.log.Error("设置REGNO_GIF页码异常", exception);
        }
        finally
        {
            dbh.Dispose();
        }  
    }
}

